package xyz.wpxy.wxpushmsg.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import xyz.wpxy.wxpushmsg.constant.WxMsgConstant;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: http发送
 * @author: Peng Wei
 * @date:2023/8/7 16:05
 */
public class HttpClientUtils {

    /**
     * http get
     *
     * @param url 可带参数的 url 链接
     */
    public static String get(String url) {
        HttpClient httpClient = HttpClients.createDefault();
        HttpResponse httpResponse = null;
        String result = "";
        HttpGet httpGet = new HttpGet(url);
        //设置超时时间:
        httpGet.setConfig(getRequestConfig());
        try {
            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity, "utf-8");

            }
        } catch (IOException e) {
            e.printStackTrace();

        }
        return result;
    }

    /**
     * http simple post**
     *
     * @param url     路径
     * @param content 内容
     * @return result
     */
    public static String httpClientByPost(String url, String content) {
        HttpClient httpClient = HttpClients.createDefault();

        HttpResponse httpResponse = null;
        String result = "";

        HttpPost httpPost = new HttpPost(url);
        //设置超时时间:
        httpPost.setConfig(getRequestConfig());
        //获取请求头:
        Map<String, String> headMaps = new HashMap<>();
        headMaps.put("Accept", "application/json");
        headMaps.put("Content-type", "application/json");
        //添加到header中:
        headMaps.forEach(httpPost::addHeader);
        try {
            StringEntity s = new StringEntity(content, "utf-8");
            httpPost.setEntity(s);
            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity, "utf-8");

            }
        } catch (IOException e) {
            e.printStackTrace();

        }
        return result;
    }


    /**
     * 获取超时时间配置
     *
     * @return RequestConfig
     */
    private static RequestConfig getRequestConfig() {
        return RequestConfig.custom()
                //超时时间【连接目标服务器超时时间：ConnectionTimeout-->指的是连接一个url的连接等待时间】:
                .setConnectTimeout(WxMsgConstant.CONNECT_TIMEOUT)
                //连接请求超时时间:
                .setConnectionRequestTimeout(WxMsgConstant.CONNECTION_REQUEST_TIMEOUT)
                //Socket超时时间【读取目标服务器数据超时时间：SocketTimeout-->指的是连接上一个url，获取response的返回等待时间】:
                .setSocketTimeout(WxMsgConstant.SOCKET_TIMEOUT)
                .build();
    }

}
